import os
import time

from Task_flow import _main
from InitialPositionOfGuide import *
from red_mark_detection import save_camera
from GTAPI import GtMainAuto
from ctypes import *
from RobotArmAPI import arm
import dxomark.analyzer.lightingcontrol as lc

from red_mark_detection import align
from take_photo import video_capture, get_image_aspect_ratio

ColorChecker_align = [{'id': 1, 'is_marker': 0, 'marker_x': 325, 'marker_y': 225, 'distance': 620, 'threshold': 70,
                       'blurkernel': 1, 'minarea': 0.02, 'maxarea': 0.2, 'ratio': 3.2}]
ColorChecker_1_1 = 3.2
ColorChecker_4_3 = 3.2
ColorChecker_16_9 = 3.2
DMC_JL = 3.1
Head_HDR_JL = 3.1
# DMC_JL = 2.7
Visual_Noise = [{'id': 1, 'is_marker': 0, 'marker_x': 692, 'marker_y': 425, 'distance': 1290, 'threshold': 140,
                 'blurkernel': 5, 'minarea': 0.05, 'maxarea': 0.1, 'ratio': 3.6}]
Visual_Noise_1_1 = 3.6
Visual_Noise_4_3 = 3.6
Visual_Noise_16_9 = 3.6
DeadLeaves_Noise = [{'id': 1, 'is_marker': 0, 'marker_x': 700, 'marker_y': 400, 'distance': 1400, 'threshold': 70,
                     'blurkernel': 3, 'minarea': 0.1, 'maxarea': 0.2, 'ratio': 3}]
DeadLeaves_16_9 = 1.5
Head_HDR_align = [{'id': 1, 'is_marker': 1, 'marker_x': 920, 'marker_y': 800, 'distance': 1300, 'threshold': 70,
                   'blurkernel': 1, 'minarea': 0.02, 'maxarea': 0.2, 'ratio': 3}]
MTF_JL = [{'id': 1, 'is_marker': 1, 'marker_x': 1821, 'marker_y': 1000, 'distance': 1190, 'threshold': 70,
           'blurkernel': 5, 'minarea': 0.05, 'maxarea': 0.1, 'ratio': 1.7}]

DOT_JL = [{'id': 1, 'is_marker': 1, 'marker_x': 1100, 'marker_y': 930, 'distance': 1190, 'threshold': 70,
           'blurkernel': 5, 'minarea': 0.05, 'maxarea': 0.1, 'ratio': 2.8}]
Timer_photo = [{'id': 1, 'is_marker': 0, 'marker_x': 690, 'marker_y': 400, 'distance': 700, 'threshold': 90,
                'blurkernel': 5, 'minarea': 0.1, 'maxarea': 0.15, 'ratio': 0.563}]
AF_HDR_JL = 2


# AF HDR, Blur, Timing
def Color_checker(phone, serial, main_guide_rail_position=ColorChecker,
                  light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)

        lc.ensureLightingControlIsRunning()  # Looks for Lighting
        lc.runSequence('All_Heating')
        # # algin
        RES, file_dir = align(ColorChecker_align, serial, 'zoom')
        if RES == '计算失败重新执行':
            Color_checker(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            ratio = get_image_aspect_ratio(file_dir)
            GtMainAuto.DominantRailMovement(0,
                                            main_guide_rail_position.front_back - RES[0] * ColorChecker_4_3)
            save_camera(serial)
            return lc.runSequence('stop_sec')

    except:
        print('图片异常')
        lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    #
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)


def Color_checker_video(phone, serial, main_guide_rail_position=ColorChecker,
                        light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)

        lc.ensureLightingControlIsRunning()  # Looks for Lighting
        lc.runSequence('All_Heating')
        # # algin
        RES, file_dir = align(ColorChecker_align, serial, 'video')
        if RES == '计算失败重新执行':
            Color_checker(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            ratio = get_image_aspect_ratio(file_dir)
            GtMainAuto.DominantRailMovement(0,
                                            main_guide_rail_position.front_back - RES[0] * ColorChecker_16_9)
            video_capture(serial, 10)
            return lc.runSequence('stop_sec')

    except:
        print('图片异常')
        lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    #
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)


def Visual_Noise_Video(phone, serial, main_guide_rail_position=VideoVisualNoise,
                       light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)
        lc.ensureLightingControlIsRunning()
        lc.runSequence('All_Heating')
        # # algin
        RES, file_dir = align(Visual_Noise, serial, 'video')
        if RES == '计算失败重新执行':
            Visual_Noise_Video(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            ratio = get_image_aspect_ratio(file_dir)
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * Visual_Noise[0]['ratio'])
            video_capture(serial, 10)
            return lc.runSequence('stop_sec')
    except:
        print('图片异常')


def Head_HDR(phone, serial, main_guide_rail_position=Head_HDR, light_guide_rail_position=Guide_Light_Photo):
    # try:
    _main(phone, main_guide_rail_position, light_guide_rail_position)
    lc.ensureLightingControlIsRunning()  # Looks for Lighting
    lc.runSequence('All_Heating')
    RES = align(Head_HDR_align, serial, 'zoom')
    if RES == '计算失败重新执行':
        Head_HDR(phone, serial, main_guide_rail_position, light_guide_rail_position)
    else:
        GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * Head_HDR_align[0]['ratio'])
        arm.adjust_rotation_X(-0.027)
        save_camera(serial)
        return lc.runSequence('stop_sec')
    # except:
    print('图片异常')
    lc.runSequence('stop_sec')


def Grey(phone, serial, main_guide_rail_position=GreyChart, light_guide_rail_position=Guide_Light_Photo):
    _main(phone, main_guide_rail_position, light_guide_rail_position)

    # algin
    lc.ensureLightingControlIsRunning()
    lc.runSequence('All_Heating')
    # align(AF_HDR_Blur_Timing, serial)
    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    save_camera(serial)
    return lc.runSequence('stop_sec')


def Grey_Video(phone, serial, main_guide_rail_position=GreyChart,
               light_guide_rail_position=Guide_Light_Photo):
    _main(phone, main_guide_rail_position, light_guide_rail_position)

    # algin
    lc.ensureLightingControlIsRunning()
    lc.runSequence('All_Heating')
    # align(AF_HDR_Blur_Timing, serial)
    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    video_capture(serial, 10)
    return lc.runSequence('stop_sec')


def MTF_Checkerboard(phone, serial, main_guide_rail_position=MTFCart,
                     light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)
        lc.ensureLightingControlIsRunning()
        lc.runSequence('All_Heating')
        # # algin
        RES = align(MTF_JL, serial, 'zoom')
        if RES == '计算失败重新执行':
            MTF_Checkerboard(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * MTF_JL[0]['ratio'])
            save_camera(serial)
            return lc.runSequence('stop_sec')
    except:
        print('图片异常')
        lc.runSequence('stop_sec')


def Dot_card(phone, serial, main_guide_rail_position=DotCart, light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)

        lc.ensureLightingControlIsRunning()  # Looks for Lighting
        lc.runSequence('All_Heating')
        RES = align(DOT_JL, serial, 'zoom')
        if RES == '计算失败重新执行':
            Dot_card(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * DOT_JL[0]['ratio'])
            GtMainAuto.ChartSwitching(DotCart.card_id)
            save_camera(serial)
            return lc.runSequence('stop_sec')
    except:
        print('图片异常')
        lc.runSequence('stop_sec')


def Dot_wide_card(phone, serial, multiple, main_guide_rail_position=DotCart, light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)

        lc.ensureLightingControlIsRunning()  # Looks for Lighting
        lc.runSequence('All_Heating')
        RES = align(DOT_JL, serial, 'wide_zoom')
        if RES == '计算失败重新执行':
            Dot_wide_card(phone, serial, multiple, main_guide_rail_position, light_guide_rail_position)
        else:
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * DOT_JL[0]['ratio'])
            GtMainAuto.ChartSwitching(DotCart.card_id)
            save_camera(serial)
            return lc.runSequence('stop_sec')
    except:
        print('图片异常')
        lc.runSequence('stop_sec')


def VIDEO_DeadLeaves(phone, serial, main_guide_rail_position=DeadLeaves_video,
                     light_guide_rail_position=Guide_Light_Video):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)
        lc.ensureLightingControlIsRunning()
        lc.runSequence('All_Heating')
        RES, file_dir = align(DeadLeaves_Noise, serial, 'video')
        if RES == '计算失败重新执行':
            VIDEO_DeadLeaves(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            ratio = get_image_aspect_ratio(file_dir)
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES[0] * DeadLeaves_16_9)
            video_capture(serial, 10)
            return lc.runSequence('stop_sec')


    except:
        print('图片异常')


def AF_HDR(phone, serial, main_guide_rail_position=AF_HDR_Blur_Timing,
           light_guide_rail_position=Guide_Light_Photo):
    _main(phone, main_guide_rail_position, light_guide_rail_position)
    lc.ensureLightingControlIsRunning()
    lc.runSequence('All_Heating')
    # # algin
    RES = align(Timer_photo, serial)
    if RES == '计算失败重新执行':
        Timer_Photo(phone, serial, main_guide_rail_position, light_guide_rail_position)
    else:
        GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back + RES * AF_HDR_JL)
        arm.adjust_rotation_Z(0.0888)
        arm.adjust_rotation_X(-0.1043)
        save_camera(serial)
        time.sleep(1)
        save_camera(serial)
        return lc.runSequence('stop_sec')


def DMC(phone, serial, main_guide_rail_position=DXOMARKChart, light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)
        lc.ensureLightingControlIsRunning()  # Looks for Lighting
        lc.runSequence('All_Heating')
        RES = align(Visual_Noise, serial)

        if RES == '计算失败重新执行':
            DMC(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back - RES * DMC_JL)
            GtMainAuto.ChartSwitching(DXOMARKChart.card_id)
            arm.adjust_rotation_Z(0.0562)
            return save_camera(serial)
    except:
        print('图片异常')
    # lc.ensureLightingControlIsRunning()
    # lc.runSequence('All_Heating')
    # # algin
    # # align(AF_HDR_Blur_Timing, serial)
    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)

    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)


def Timer_Photo(phone, serial, main_guide_rail_position=Timing_Photo,
                light_guide_rail_position=Guide_Light_Photo):
    try:
        _main(phone, main_guide_rail_position, light_guide_rail_position)
        lc.ensureLightingControlIsRunning()
        lc.runSequence('All_Heating')
        # # algin
        RES = align(Timer_photo, serial)
        if RES == '计算失败重新执行':
            Timer_Photo(phone, serial, main_guide_rail_position, light_guide_rail_position)
        else:
            GtMainAuto.SetStopPosition_main(4)
            GtMainAuto.DominantRailMovement(0, main_guide_rail_position.front_back + RES * Timer_photo[0]['ratio'])
            arm.adjust_rotation_Z(-0.186)
            arm.adjust_rotation_X(-0.067)
            GtMainAuto.SetWorkPosition_main(5)
            save_camera(serial)
            return lc.runSequence('stop_sec')
    except:
        print('图片异常')
        lc.runSequence('stop_sec')

    # lc.ensureLightingControlIsRunning()
    # lc.runSequence('All_Heating')
    # # algin
    # # align(AF_HDR_Blur_Timing, serial)
    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)


def VIDEO_Timing(phone, serial, main_guide_rail_position=Timing,
                 light_guide_rail_position=Guide_Light_Video):
    _main(phone, main_guide_rail_position, light_guide_rail_position)
    # lc.ensureLightingControlIsRunning()
    # lc.runSequence('All_Heating')
    # # algin
    # align(AF_HDR_Blur_Timing, serial)
    # lc.runSequence('stop_sec')
    # upload_file = '2023-12-07-22-24-30'
    # lc.ensureLightingControlIsRunning()  # Looks for Lighting
    # lc.setLuxmeters('GO00535', 'GO00534')  # sideGOSSEN centerGOSSEN
    # lc.calibrate('load', r'F:\lightlist\{}'.format(upload_file))
    # # sec_name = 'Lux_' + str(light) + '_'+str(lux)
    # sec_name = 'My_{}_{}lux'.format(light, lux)
    # lc.runSequence(sec_name)


if __name__ == '__main__':
    lc.runSequence('Trigger')
    # AF_HDR(2, '27021FDH3001L0 ','D65', '500')
    # Grey(6, 'R4SW4LUW6HW8VCEI ', 'D65', '500')
    # VIDEO_DeadLeaves(1, '1B231FDEE001CS ', 'D65', '500')
    # VIDEO_DeadLeaves(2, '27021FDH3001L0 ', 'D65', '500')
    # Grey_Video(1, '1B231FDEE001CS ', 'D65', '500')
    # Grey_Video(2, '27021FDH3001L0 ', 'D65', '500')
    # Head_HDR(3, '28311FDH3000SF ', 'D65', '500')
    # Head_HDR(4, '3A091FDJG007P7 ', 'D65', '500')
    # Head_HDR(5, 'R58M74FN27R ', 'D65', '500')
    # Head_HDR(6, 'R4SW4LUW6HW8VCEI ', 'D65', '500')
    # Color_checker_video(1, '1B231FDEE001CS ', 'D65', '500')
    # Color_checker_video(2, '27021FDH3001L0 ', 'D65', '500')
    # Color_checker_video(3, '28311FDH3000SF ', 'D65', '500')
    # Color_checker_video(4, '3A091FDJG007P7 ', 'D65', '500')
    # Color_checker_video(5, 'R58M74FN27R ', 'D65', '500')
    # # Color_checker_video(6, 'R4SW4LUW6HW8VCEI ', 'D65', '500')
    # Visual_Noise_Video(1, '1B231FDEE001CS ', 'D65', '500')
    # Visual_Noise_Video(2, '27021FDH3001L0 ', 'D65', '500')
    # Visual_Noise_Video(3, '28311FDH3000SF ', 'D65', '500')
    # Visual_Noise_Video(4, '3A091FDJG007P7 ', 'D65', '500')
    # Visual_Noise_Video(5, 'R58M74FN27R ', 'D65', '500')
    # Visual_Noise_Video(6, 'R4SW4LUW6HW8VCEI ', 'D65', '500')

# Color_checker(6, 'FMR0223825050927 ', 'D65', '500')

# align(ColorChecker_align, 'd364e08d')
# Grey(6, '81a8e14c ', 'D65', '500')
# AF_HDR(6, 'd364e08d', 'D65', '500')
